import { DB } from "../db"
import { ServerValues } from "../../../kds-base-define/src/ServerConfig"
import { Rpc } from "../rpc"
import { kdRpcMsg } from "kdweb-core/lib/rpc/protocols"
import { Log } from "../log"
import { RoomDefine } from "../../../kds-base-define/src/RoomDefine"
import { UserDefine } from "../../../kds-base-define/src/UserDefine"
import { kdutils } from "kdweb-core/lib/utils"
import { mongoDB } from "kdweb-core/lib/mongo/controller"
import { RoomCodeID } from "./room_id_code"

let db = DB.get(ServerValues.dbAdmin)
let adminTableName = "t_login"
async function getAdminLogin(h: string, params: {
    username: string,
    password: string
}) {
    let loginData = await db.getSingle(adminTableName,params)
	if(loginData) {
		return loginData
	}
	return null
}

async function getUserInfo(h: string, params: {
   userId: string
}) {
    let loginData = await db.getSingle(adminTableName,params)
	if(loginData) {
		return loginData
	}
	return null
}

/** type: 1 年活，2 月活，3  年月活 */
async function getVisit(h: string, type: number) {
	let db = DB.get("tds-record","record")
	var tYear = new Date().getFullYear()
	var tMonth = new Date().getMonth() //当前月份
	var startTime = new Date(tYear, 0, 1).getTime()
    var yearStart = new Date(tYear, tMonth, 1).getTime()//当前月第一天
	var yearEnd = new Date(tYear, tMonth + 1, 0).getTime() //当前月最后一天
    let n  = { time : { "$gte": startTime} };
    let y  = { time : { "$gte": yearStart} };
	let nian = await db.getOption('t_login', n);
	let yue = await db.getOption('t_login', y);
	let calldata = {
		nian: nian,
		yue: yue
	}
	return calldata
}

async function getUserList(h: string, params:{
	page: number,
	pageSize: number
}) {
	// let db = DB.get(ServerValues.dbAdmin)
	// let data = DB.login.getOption("t_login",{})
	let data = await DB.lobby.getOption(DB.t_userInfo, {})
	return data;
}

// room.tea
export let RpcAdminTea = {
    login: getAdminLogin,
    getUserInfo: getUserInfo,
	getVisit: getVisit,
	getUserList: getUserList
}
